package com.tanhua.server.service;

import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.mongo.UserLike;
import com.tanhua.domain.vo.InformVo;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.InformApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.mongo.PublishApi;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.bson.types.ObjectId;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class InformService {

    @Reference
    private InformApi informApi;

    @Reference
    private UserInfoApi userInfoApi;

    @Reference
    private PublishApi publishApi;


    /**
     * 喜欢通知列表
     * @param page
     * @param pagesize
     * @return
     */
    public ResponseEntity<Object> loveMessage(Integer page, Integer pagesize) {
        //获取当前用户id
        Long userId = UserHolder.getUserId();
        //调用api获取分页数据
        PageResult pageResult = informApi.findLoveById(page,pagesize,userId);
        List<UserLike> userLikeList = (List<UserLike>) pageResult.getItems();
        //封装数据
        List<InformVo> informVoList = new ArrayList<>();
        for (UserLike userLike : userLikeList) {
            InformVo informVo = new InformVo();
            //获取用户资料
            UserInfo userInfo = userInfoApi.findById(userLike.getUserId());
            if(userInfo!=null){
                BeanUtils.copyProperties(userInfo,informVo);
            }
            informVo.setId(userLike.getUserId().toString());
            informVo.setCreateDate(new SimpleDateFormat("yyyy-MM-dd").format(userLike.getCreated()));
            informVoList.add(informVo);
        }
        pageResult.setItems(informVoList);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 点赞通知列表
     * @param page
     * @param pagesize
     * @return
     */
    public ResponseEntity<Object> likeMessage(Integer page, Integer pagesize) {
        //获取当前用户id
        Long userId = UserHolder.getUserId();
        //查询用户发送的动态id
        List<ObjectId> objectIdList = publishApi.findByAlbum(userId);
        //调用api根据动态id查询点赞
        PageResult pageResult = informApi.findLikeById(objectIdList,1,page,pagesize);
        setInformVoList(pageResult);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 评论通知列表
     * @param page
     * @param pagesize
     * @return
     */
    public ResponseEntity<Object> commentMessage(Integer page, Integer pagesize) {
        //获取当前用户id
        Long userId = UserHolder.getUserId();
        //查询用户发送的动态id
        List<ObjectId> objectIdList = publishApi.findByAlbum(userId);
        //调用api根据动态id查询点赞
        PageResult pageResult = informApi.findLikeById(objectIdList,2,page,pagesize);
        setInformVoList(pageResult);
        return ResponseEntity.ok(pageResult);
    }

    private void setInformVoList(PageResult pageResult){
        List<Comment> commentList = (List<Comment>) pageResult.getItems();
        //封装数据返回
        List<InformVo> informVoList = new ArrayList<>();
        for (Comment comment : commentList) {
            InformVo informVo = new InformVo();
            //获取用户资料
            UserInfo userInfo = userInfoApi.findById(comment.getUserId());
            if(userInfo!=null){
                BeanUtils.copyProperties(userInfo,informVo);
            }
            informVo.setId(comment.getUserId().toString());
            informVo.setCreateDate(new SimpleDateFormat("yyyy-MM-dd").format(comment.getCreated()));
            informVoList.add(informVo);
        }
        pageResult.setItems(informVoList);
    }
}
